var SPSdk = (function () {
    var SDK = "http://static.sy.kugou.com/lib/KGMG/SDK/lastest/kgm-sdk.js";

    function SPSdk() {
        this.access_token = "";
        this.appId = "";
        this.appKey = "";
        this.loginKey = "";
        this.openId = 0;
        this.openKey = "";
        this.payKey = "";
        this.spFname = "";
        this.spId = 0;
        this.other = "";
    }

    var __proto = SPSdk.prototype;

    __proto.login = function (param, callback) {
        console.log("酷狗登录：" + JSON.stringify(param));
        var m_this = this;
        kgmgsdk.invokeLogin( {
            callback: function( ress ) {
                if(ress.status == 1)
                {
                    kgmgsdk.getUserInfo( {
                        callback    : function( res ) {
                            switch ( res.status ) {
                                case 1 :
                                    callback && callback({"result":0,"spuid":res.data.userid,"nickname":res.data.username});
                                    break;
                                default :
                                    callback && callback({"result":-2,desc:"faile"});
                                    break;
                            }
                        }
                    } );
                }else
                {
                    callback && callback({"result":ress.status,desc:ress.msg});
                }
            }
        } );

    };
    __proto.share = function (param, callback) {
        console.log("酷狗分享：" + JSON.stringify(param));
        var m_this = this;
        kgmgsdk.share( {title:param.title,intro: param.desc,image: param.imgsrc,link: param.link,
            callback: function( res )
            {
                if(res == 1)
                    callback && callback({"result":0,"desc":"success"});
                else
                    callback && callback({"result":-1,"desc":res.msg});
            }
        } );
    };
    __proto.pay = function (param, callback) {
        var m_this = this;
        console.log("酷狗支付：" + JSON.stringify(param));

        kgmgsdk.openPay({
            appid       : m_this.appId,
            merchantid  : m_this.other,
            gameid      : m_this.loginKey,
            serverid    :"1",
            gamename    :"layabox",
            role         :"layabox",
            extension1  :param.data.other.orderId,
            extension2  :param.params,
            money       : parseInt(param.data.amount/100),
            pay4game   : true,
            callback    : function( res ) {
                switch ( res.status ) {
                    case 1 :
                        callback && callback({"result": 0, "desc": "OK", "sp_data": res.data});
                        break;
                    default :
                        callback && callback({"result": res.status, "desc": res.msg});
                        break;
                }
            }
        })
    };

    __proto.init = function (param, callback) {
        console.log("酷狗初始化：" + JSON.stringify(param));
        var m_this = this;
        m_this.access_token = param.access_token;
        m_this.appId = param.appId;
        m_this.appKey = param.appKey;
        m_this.loginKey = param.loginKey;
        m_this.openId = param.openId;
        m_this.openKey = param.openKey;
        m_this.payKey = param.payKey;
        m_this.spFname = param.spFname;
        m_this.spId = param.spId;
        m_this.other = param.other;

        LayaCommon.loadScript(SDK+"?"+Math.random(), function () {
            kgmgsdk.init({
                appid: m_this.appId,
                merchantid: m_this.other,
                gameid: m_this.loginKey,
                serverid: "1",
                gamename: "",
                callback: function (res) {
                    switch (res.status) {
                        case 1:
                            callback && callback({"result": 0, "desc": "OK", "sp_data": res.data});
                            break;
                        default:
                            callback && callback({"result": -2, "desc": res.msg});
                            break;
                    }
                }
            });
        }, function () {
            callback({"ret": -1, "msg": "plugin download error"});
        });
    };
    return SPSdk;
})();